package com.example.kotlin01

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.example.kotlin01.model.User
import com.example.kotlin01.viewmodel.UserViewModel
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
    val userViewModel:UserViewModel = UserViewModel(MyApplication.getInstance())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv_add.setOnClickListener {
           val user = User(Date().time,"小明","男")
            userViewModel.addUser(user)

        }
        tv_delete.setOnClickListener {
            val user = User(1,"小明","男")
            userViewModel.deleteUser(user)
        }
        tv_query.setOnClickListener {
            userViewModel.queryUser()

        }
        userViewModel.userLiveData.observe(this, Observer {
            Log.e("user_room", "查询结果：$it" )
        })

    }

}